package com.dzeshko.auth;

import java.io.IOException;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.dzeshko.AuthActivity;
import com.dzeshko.content.FeedContentManager;
import com.dzeshko.http.HttpManager;
import com.dzeshko.json.ListJsonObject;

import extent.ParamCallback;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.widget.Toast;

public class GplusAuthHelper {
	
	public final static String ACCESS_TOKEN = "access_token";
	
	public static String AUTH_PREFS = "auth_prefs";
	
	private static String mAuthCode;
	
	private static final String mClientId = "305414411924.apps.googleusercontent.com";
	
	private static final String mApiKey = "AIzaSyBn8RC1cunEe9jhXYgRHf2UggCJl6qZxhA";
	
	private static final String mScope = "https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+"+
										"https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&";
	
	private static final String mRedirectUri = "urn:ietf:wg:oauth:2.0:oob";
	
	private static final String mResponseUrl = "https://accounts.google.com/o/oauth2/auth?"+
												"scope=" + mScope +												
												"redirect_uri=" + mRedirectUri + "&" +
												"response_type=code&" +
												"client_id=" + mClientId;
	
	private static String POST = "/o/oauth2/token HTTP/1.1";
	private static String Host = "accounts.google.com";
	private static String ContentType = "application/x-www-form-urlencoded";
												
	private static final String mAuthRequest ="code=" + mAuthCode + "&"+
												"client_id=" + mClientId + "&"+
												"client_secret={client_secret}&"+
												"redirect_uri=https://oauth2-login-demo.appspot.com/code&"+
												"grant_type=authorization_code";
	
	public static GplusAuthHelper instance;

	
	private GplusAuthHelper(){}
	
	public static GplusAuthHelper getInstance(){
		if(instance==null){
			instance = new GplusAuthHelper();
		}
		return instance;
	}
	
	public static void saveCredentials(Context ctx, String token) {
		Editor editor = ctx.getSharedPreferences(AUTH_PREFS, Context.MODE_PRIVATE).edit();
		editor.putString(ACCESS_TOKEN, token);
		editor.commit();
	}
	
	public static boolean isLogged(Context ctx) {
		SharedPreferences savedSession = ctx.getSharedPreferences(AUTH_PREFS, Context.MODE_PRIVATE);
		return savedSession.getString(ACCESS_TOKEN, null) != null;
	}
	
	
	/*public static boolean sign(Context ctx, String title) {
		
		mAuthCode = title.substring(title.indexOf("=")+1);
		final JSONObject jsonWithToken;
		FeedContentManager.getInstance().getToken(new ParamCallback<JSONObject>() {
			
			@Override
			public void onSuccess(JSONObject p) {
				// TODO Auto-generated method stub
				jsonWithToken = p;
			}
			
			@Override
			public void onError(Throwable e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		
		String token = "";												
		try {															
				//
		token = jsonWithToken.getString(ACCESS_TOKEN);					
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			Log.e("JSONexcept", e.getMessage());
			Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG).show();
			e.printStackTrace();
			return false;
		}
		
		
		SharedPreferences savedSession = ctx.getSharedPreferences(AUTH_PREFS, Context.MODE_PRIVATE);
		Editor edit = savedSession.edit();
		if(!token.equals("")){
		edit.putString(ACCESS_TOKEN, jsonWithToken.getString(ACCESS_TOKEN));
		edit.commit();
		Log.i("showprefs", savedSession.getString(ACCESS_TOKEN, ""));
		}
		else {
			Log.e("ACCESS_ERROR", "no token");
			Toast.makeText(ctx, "token not found", Toast.LENGTH_LONG).show();
			return false;
		}
		return true;
	}
	*/
	
	
	public static void exit(Context context) {
		// Edge case: an illegal state exception is thrown if an instance of
		// CookieSyncManager has not be created. CookieSyncManager is normally
		// created by a WebKit view, but this might happen if you start the
		// app, restore saved state, and click logout before running a UI
		// dialog in a WebView -- in which case the app crashes
		try{
			CookieSyncManager cookieSyncMngr = CookieSyncManager
					.createInstance(context);
			CookieManager cookieManager = CookieManager.getInstance();
			cookieManager.removeAllCookie();
		} catch (Exception e) {
			// TODO: handle exception
		}
		Editor editor = context.getSharedPreferences(AUTH_PREFS, Context.MODE_PRIVATE)
				.edit();
		editor.clear();
		editor.commit();
	}

	
	public static final String getResponseUrl() {
		return mResponseUrl;
	}
	
	public static String getAuthCode() {
		return mAuthCode;
	}

	public static void setAuthCode(String code) {
		
		mAuthCode = code.substring(code.indexOf("=")+1);
	}

	public String getClientId() {
		return mClientId;
	}

	public String getRedirectUri() {
		return mRedirectUri;
	} 
}
